package cgroups

import (
	"JoDocker/cgroups/subsystems"
	"github.com/Sirupsen/logrus"
)

type CGroupManager struct {
	Path        string
	ResourcePtr *subsystems.ResourceConfig
}

func MakeCGroupManager(path string) *CGroupManager {
	return &CGroupManager{
		Path:        path,
		ResourcePtr: nil,
	}
}

func (this *CGroupManager) Apply(pid int) error {
	for _, subSysInst := range subsystems.SubSystemInstSlc {
		subSysInst.Apply(this.Path, pid)
	}
	return nil
}

func (this *CGroupManager) Set(resConfigPtr *subsystems.ResourceConfig) error {
	for _, subSysInst := range subsystems.SubSystemInstSlc {
		subSysInst.Set(this.Path, resConfigPtr)
	}
	return nil
}

func (this *CGroupManager) Destory() error {
	for _, subSysInst := range subsystems.SubSystemInstSlc {
		if err := subSysInst.Remove(this.Path); err != nil {
			logrus.Warnf("remove cgroup fail %v", err)
		}
	}
	return nil
}
